package cn.prophet.service.impl;

import cn.prophet.bean.BankCard;
import cn.prophet.bean.BankCardExample;
import cn.prophet.bean.User;
import cn.prophet.bean.vo.AliBankBean;
import cn.prophet.mapper.BankCardMapper;
import cn.prophet.mapper.UserMapper;
import cn.prophet.service.BankCardService;
import cn.prophet.service.ForeignService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class BankCardServiceImpl implements BankCardService{
    @Resource
    private ForeignService foreignService;
    @Resource
    private UserMapper userMapper;
    @Resource
    private BankCardMapper bankCardMapper;
    @Override
    public int addBankCard(Long userId, String userName, String bankCard,String bankName) {
        User user = userMapper.selectByPrimaryKey(userId);
        if (user==null){
            //用户不存在
            return 2;
        }
        List<BankCard> bankCardList = getBankCard(userId);
        if(bankCardList!=null&&bankCardList.size()>=1){
            return 3;
        }
        BankCard card = new BankCard();
        card.setUserId(userId);
        card.setBankName(bankName);
        card.setBankNum(bankCard);
        card.setBankUserName(userName);
        card.setCreatetime(new Date());
        card.setBankType((byte) 1);

        return bankCardMapper.insertSelective(card);
    }

    @Override
    public AliBankBean getBankName(String bankNum) {


        return foreignService.getBankName(bankNum);
    }

    @Override
    public List<BankCard> getBankCard(Long userId) {
        BankCardExample bankCardExample = new BankCardExample();
        BankCardExample.Criteria bankCriteria = bankCardExample.createCriteria();
        bankCriteria.andUserIdEqualTo(userId);
        bankCriteria.andIsdeleteEqualTo((byte) 0);
        List<BankCard> bankCardList = bankCardMapper.selectByExample(bankCardExample);

        return bankCardList;
    }

    @Override
    public int releaseBankCard(Long cardId) {

        BankCard card = bankCardMapper.selectByPrimaryKey(cardId);

        if(card==null){
            return 2;
        }
        if(card.getIsdelete()==1){
            return 3;
        }
        BankCard bankCard = new BankCard();
        bankCard.setIsdelete((byte) 1);
        bankCard.setId(cardId);

        return bankCardMapper.updateByPrimaryKeySelective(bankCard);
    }
}
